﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProduceBasicData.Infrastructure
{
    /// <summary>
    /// 城市编码服务（提供20+中国城市的编码映射）
    /// </summary>
    public interface ICityCodeService
    {
        /// <summary>
        /// 查询所有支持的城市编码（含名称）
        /// </summary>
        Task<List<CityInfo>> QueryAllCityCodesAsync();

        /// <summary>
        /// 根据城市名称查询编码（模糊匹配）
        /// </summary>
        Task<CityInfo?> QueryCityCodeByNameAsync(string cityName);
    }

    /// <summary>
    /// 城市编码服务实现（实际项目可扩展为从数据库/配置中心读取）
    /// </summary>
    public class CityCodeService : ICityCodeService
    {
        /// <summary>
        /// 20+中国主要城市编码（高德API标准编码）
        /// </summary>
        private readonly List<CityInfo> _cityCodes = new()
        {
            new CityInfo { CityName = "北京", CityCode = "110000" },
            new CityInfo { CityName = "上海", CityCode = "310000" },
            new CityInfo { CityName = "广州", CityCode = "440100" },
            new CityInfo { CityName = "深圳", CityCode = "440300" },
            new CityInfo { CityName = "杭州", CityCode = "330100" },
            new CityInfo { CityName = "南京", CityCode = "320100" },
            new CityInfo { CityName = "成都", CityCode = "510100" },
            new CityInfo { CityName = "重庆", CityCode = "500000" },
            new CityInfo { CityName = "武汉", CityCode = "420100" },
            new CityInfo { CityName = "西安", CityCode = "610100" },
            new CityInfo { CityName = "天津", CityCode = "120000" },
            new CityInfo { CityName = "苏州", CityCode = "320500" },
            new CityInfo { CityName = "宁波", CityCode = "330200" },
            new CityInfo { CityName = "厦门", CityCode = "350200" },
            new CityInfo { CityName = "青岛", CityCode = "370200" },
            new CityInfo { CityName = "大连", CityCode = "210200" },
            new CityInfo { CityName = "沈阳", CityCode = "210100" },
            new CityInfo { CityName = "长沙", CityCode = "430100" },
            new CityInfo { CityName = "郑州", CityCode = "410100" },
            new CityInfo { CityName = "济南", CityCode = "370100" },
            new CityInfo { CityName = "合肥", CityCode = "340100" },
            new CityInfo { CityName = "福州", CityCode = "350100" },
            new CityInfo { CityName = "昆明", CityCode = "530100" },
            new CityInfo { CityName = "贵阳", CityCode = "520100" },
            new CityInfo { CityName = "石家庄", CityCode = "130100" },
            new CityInfo{CityName="曲阳",CityCode="130634"},
            new CityInfo { CityName = "巨鹿", CityCode = "130529" },
             new CityInfo { CityName = "冠县", CityCode = "371525" },
             new CityInfo { CityName = "川汇区", CityCode = "411602" },
             new CityInfo { CityName = "六安", CityCode = "341500" },
            new CityInfo { CityName = "黄山", CityCode = "341000" },
            new CityInfo { CityName = "阜阳", CityCode = "341200" },
            new CityInfo { CityName = "亳州", CityCode = "341600" },
            new CityInfo { CityName = "蚌埠", CityCode = "340300" },
            new CityInfo { CityName = "芜湖", CityCode = "340200" },
        };

        public async Task<List<CityInfo>> QueryAllCityCodesAsync()
        {
            // 模拟异步操作
            await Task.CompletedTask;
            return _cityCodes;
        }

        public async Task<CityInfo?> QueryCityCodeByNameAsync(string cityName)
        {
            await Task.CompletedTask;
            // 模糊匹配（如输入“北”可匹配“北京”）
            return _cityCodes.FirstOrDefault(c => c.CityName.Contains(cityName.Trim()));
        }
    }

    /// <summary>
    /// 城市信息实体（编码+名称映射）
    /// </summary>
    public class CityInfo
    {
        /// <summary>城市名称（如：北京）</summary>
        public string CityName { get; set; } = string.Empty;

        /// <summary>高德API城市编码（如：北京=110000）</summary>
        public string CityCode { get; set; } = string.Empty;
    }
}
